//
//  MoviesViewController.m
//  DaHaiDouBan
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 Wang Dahai. All rights reserved.
//

#import "MoviesViewController.h"
#import "MovieListCell.h"
#import "AppTool.h"
#import "Movie.h"
#import "MovieDetailViewController.h"
#import "MBProgressHUD.h"
@interface MoviesViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView *movieTableView;
@property (nonatomic, retain) NSMutableArray *movieArray;
@property (nonatomic, retain) MBProgressHUD *hud;
@end

@implementation MoviesViewController
- (void)dealloc
{
    [_movieTableView release];
    [_hud release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.title = @"电影";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav@2x"] forBarMetrics:UIBarMetricsDefault];
    self.movieTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    
    self.movieTableView.delegate = self;
    self.movieTableView.dataSource = self;
    [self.view addSubview:self.movieTableView];
    [_movieTableView release];
    //注册 cell
    [self.movieTableView registerClass:[MovieListCell class] forCellReuseIdentifier:@"cell"];
    
#pragma mark -- 归档第四步:创建 model 对象进行归档操作
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *movieArrayPath = [document stringByAppendingPathComponent:@"active.xml"];
    
    self.movieArray = [NSKeyedUnarchiver unarchiveObjectWithFile:movieArrayPath];
    if (self.movieArray.count == 0) {
        
        //数据请求
        NSString *url_str = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
        self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.hud.labelText = @"加载中......";
        
        //初始化数组
        self.movieArray = [NSMutableArray array];
        [AppTool getDataWithAFNFromURL:url_str andBlock:^(id result) {
            
            NSLog(@"================%@",result);
            NSMutableDictionary *dic = result;
            NSString *error_code = [dic objectForKey:@"error_code"];
            NSString *reason = [dic objectForKey:@"reason"];
            NSMutableArray *array = [dic objectForKey:@"result"];
            if ([reason isEqualToString:@"success"]) {
                for (NSMutableDictionary *smallDic in array) {
                    Movie *movie = [[Movie alloc] init];
                    
                    [movie setValuesForKeysWithDictionary:smallDic];
                    
                    [self.movieArray addObject:movie];
                    
                    [movie release];
                    
                }
                
                
            }else{
                
                NSLog(@"数据请求失败");
                
            }
            /************一定要在有数据之后刷新************/
            [self.movieTableView reloadData];
            [self.hud hide:YES];
            //对 model 进行归档,存入沙盒
            [NSKeyedArchiver  archiveRootObject:self.movieArray     toFile:movieArrayPath];
            
            
        }];

        
        
    }
    

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.movieArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndetifier = @"cell";
    MovieListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];
    
    cell.movie = [self.movieArray objectAtIndex:indexPath.row];
    
    return cell;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 145;
}
//cell的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //取消 cell 的点击置灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MovieDetailViewController *detailVC = [[MovieDetailViewController alloc] init];
    
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    
    detailVC.movieID = movie.movieId;
    
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
